package com.beasts.ant.page.controller;

import com.beasts.ant.page.common.dto.AntUCDTO;
import com.beasts.ant.page.common.token.AntUCToken;
import com.beasts.ant.page.utils.json.JacksonUtil;
import com.beasts.ant.page.utils.servlet.CookieUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 入口
 * @author Locker
 * @date 07/05/2018 8:39 AM
 * @since 1.0
 */
@Controller
public class LoginController {

    /**
     * 登录页
     * @return
     */
    @RequestMapping({"/", "/login"})
    public String login() {
        return "homePage";
    }

    /**
     * 登录验证
     * @return
     */
    @RequestMapping({ "/loginCheck"})
    @ResponseBody
    public Map<String, Object> authLogin(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Map<String, Object> map = new HashMap<>();
        try {

            String fullToken = AntUCToken.getToken(username, password);
            String token = (String) JacksonUtil.getProperty(fullToken, "access_token");
            map.put("token", token);
            map.put("success", true);
            map.put("message", "登录成功");
        }  catch (Exception e) {
            map.put("success", false);
            map.put("message", "登录失败，用户名或密码错误！");
            return map;
        }

        return map;
    }

    /**
     * 查询用户
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public Map<String, Object> findUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String tokenId = req.getParameter("tokenId");
        Map<String, Object> userMap = new HashMap<>();
        try {
            userMap = AntUCToken.getUserByToken(tokenId);
            return userMap;
        } catch (Exception e) {
            return userMap;
        }
    }


    /**
     * 首页
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "index";
    }


}
